package com.common.www.controller.recycle;

import com.alibaba.fastjson.JSON;
import com.common.www.aop.resolver.CurrentUser;
import org.springframework.boot.base.jpa.domain.PageableRequest;
import org.springframework.boot.base.jpa.service.IBaseService;
import com.common.www.bean.entity.Result;
import com.common.www.bean.entity.Sort;
import com.common.www.bean.entity.auth.AuthUser;
import com.common.www.bean.entity.recycle.*;
import com.common.www.bean.vo.UserVo;
import com.common.www.data.NetCode;
import com.common.www.service.auth.UserService;
import com.common.www.service.recycle.*;
import com.common.www.utils.ParamsUtils;
import com.common.www.utils.R;
import com.common.www.utils.SortUtils;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@CrossOrigin()
@Transactional
@RequestMapping(value = {"api/recycle"})
@Api(tags = "API 回收品类管理接口")
public class RecycleController {
    @Autowired
    private RecycleTypeService recycleTypeService;
    @Autowired
    private RecycleBillService recycleBillService;
    @Autowired
    private RecycleBillTypeItemService recycleBillTypeItemService;
    @Autowired
    private RecycleBillImgItemService recycleBillImgItemService;
    @Autowired
    private UserService userService;
    @Autowired
    private RecycleTypePriceService recycleTypePriceService;

    protected IBaseService getService() {
        return recycleTypeService;
    }


    @ApiOperation("手机端查询品类列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/queryRecycleType", method = RequestMethod.POST)
    public Result queryRecycleType(Integer level, Integer page, Integer size,String city) {
        if (ParamsUtils.isInputEmpty()) {
            return R.fail(NetCode.CODE_INPUT_OMIT);
        }
        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 20;
        }
        HashMap hashMap = new HashMap();
        hashMap.put("flag", 1);
        if (level > 0) {
            hashMap.put("level", level);
        }
//        if (level == 0 || level == 2) {
//            hashMap.put("minPrice_gt", 0);
//            hashMap.put("maxPrice_gt", 0);
//        }
//        hashMap.put("unitType_gt", 0);
        PageableRequest pageableRequest = new PageableRequest();
        pageableRequest.setPage(page - 1);
        pageableRequest.setSize(size);
        pageableRequest.setSort(SortUtils.addSort(new Sort("createTime", "ASC")));
        List<RecycleType> recycleTypeList = recycleTypeService.getList(hashMap, pageableRequest);
        return R.ok(recycleTypeList);
    }

    @ApiOperation("手机端查询回收品类二级、三级列表---分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/queryRecycleTypeSecondLevelToPage", method = RequestMethod.POST)
    public Result queryRecycleTypeSecondLevelToPage(Integer page, Integer size, @CurrentUser UserVo userVo) throws Exception {
        if (ParamsUtils.isInputEmpty()) {
            return R.fail(NetCode.CODE_INPUT_OMIT);
        }
        if (userVo.getCode() == null){
            throw new Exception("查询出错，未获取到用户城市信息");
        }

        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 20;
        }
        HashMap hashMap = new HashMap();
        hashMap.put("flag", 1);
        hashMap.put("level", 2);

        PageableRequest pageableRequest = new PageableRequest();
        pageableRequest.setPage(page - 1);
        pageableRequest.setSize(size);
        pageableRequest.setSort(SortUtils.addSort(new Sort("createTime", "ASC")));
        List<RecycleType> recycleTypeList = recycleTypeService.getList(hashMap, pageableRequest);

        for (RecycleType tempType : recycleTypeList){
            HashMap map = new HashMap();
            map.put("recycleType.id",tempType.getId());
            map.put("code",userVo.getCode());
            List<RecycleTypePrice> typePrices = recycleTypePriceService.getList(map,null);
            if (typePrices.size() > 0){
                tempType.setItemPrice(typePrices.get(0));
            }
        }
        return R.ok(recycleTypeList);
    }



    @ApiOperation("手机端查询回收品类二级、三级列表---不分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/queryRecycleTypeSecondLevel", method = RequestMethod.POST)
    public Result queryRecycleTypeFirstLevel(String typeId, Integer page, Integer size, @CurrentUser UserVo userVo) throws Exception {
        if (ParamsUtils.isInputEmpty()) {
            return R.fail(NetCode.CODE_INPUT_OMIT);
        }
        if (StringUtils.isEmpty(typeId)){
            throw new Exception("参数错误，未指明查询内容");
        }
        if (userVo.getCode() == null){
            throw new Exception("查询出错，未获取到用户城市信息");
        }

        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 20;
        }
        HashMap hashMap = new HashMap();
        hashMap.put("flag", 1);
        hashMap.put("parent.id", typeId);

        PageableRequest pageableRequest = new PageableRequest();
        pageableRequest.setPage(page - 1);
        pageableRequest.setSize(size);
        pageableRequest.setSort(SortUtils.addSort(new Sort("createTime", "ASC")));
        List<RecycleType> recycleTypeList = recycleTypeService.getList(hashMap, null);

        for (RecycleType tempType : recycleTypeList){
            HashMap map = new HashMap();
            map.put("recycleType.id",tempType.getId());
            map.put("code",userVo.getCode());
            List<RecycleTypePrice> typePrices = recycleTypePriceService.getList(map,null);
            if (typePrices.size() > 0){
                tempType.setItemPrice(typePrices.get(0));
            }
        }
        return R.ok(recycleTypeList);
    }

    @ApiOperation("手机端查询回收单信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/queryBillDataList", method = RequestMethod.GET)
    public Result queryBillDataList(String recycleBillId, Integer status, Integer tabType, Integer page, Integer size, @CurrentUser UserVo userVo) {
        if (ParamsUtils.isInputEmpty()) {
            return R.fail(NetCode.CODE_INPUT_OMIT);
        }
        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 20;
        }

        HashMap hashMap = new HashMap();
        if (StringUtils.isNotEmpty(recycleBillId)) {
            hashMap.put("id", recycleBillId);
        }
        if (status != null) {
            hashMap.put("status", status);
        }
        if (tabType != null) {
            if (tabType == 1) {
                Integer[] commonValues = {0, 1};
                hashMap.put("status_in", commonValues);
            } else if (tabType == 2) {
                Integer[] commonValues = {10, 11};
                hashMap.put("status_in", commonValues);
            }
        }

        PageableRequest pageableRequest = new PageableRequest();
        pageableRequest.setPage(page - 1);
        pageableRequest.setSize(size);
        if (status == null) {
            pageableRequest.setSort(SortUtils.addSort(new Sort("status", "asc")));
        } else {
            pageableRequest.setSort(SortUtils.addSort(new Sort("createTime", "desc")));
        }
        List<RecycleBill> itemList = recycleBillService.getList(hashMap, pageableRequest);
        return R.ok(itemList);
    }

    @ApiOperation("手机端查询回收单下回收品类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/queryRecycleBillTypeItem", method = RequestMethod.POST)
    public Result queryRecycleBillTypeItem(String recycleBillId, Integer level, @CurrentUser UserVo userVo) {
        HashMap hashMap = new HashMap();
        hashMap.put("parent.id", recycleBillId);
        if (level > 0) {
            hashMap.put("level", level);
        }
        PageableRequest pageableRequest = new PageableRequest();
        pageableRequest.setSort(SortUtils.addSort(new Sort("createTime", "desc")));
        List<RecycleBillTypeItem> itemList = recycleTypeService.getList(hashMap, null);
        return R.ok(itemList);
    }

    @ApiOperation("提交回收单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/saveByJsonHuiShou", method = RequestMethod.POST)
    public Result saveByJsonHuiShou(String json, String pinLeiIds, @CurrentUser UserVo userVo) {
        String[] pinLeiIdsArray = null;
        if (StringUtils.isNotEmpty(pinLeiIds)) {
            pinLeiIdsArray = pinLeiIds.split(",");
        }

        RecycleBill entity = JSON.parseObject(json, RecycleBill.class);
        if (entity.getId() == null) {//只有创建的时候才同步创建楼栋
        } else {
        }
        AuthUser createUser = userService.findOne(userVo.getId());
        entity.setCreateUser(createUser);
        RecycleBill item = recycleBillService.saveAndFlush(entity);
        //添加到RecycleBillTypeItem
        HashMap hashMap = new HashMap();
        if (StringUtils.isNotEmpty(pinLeiIds)) {
            hashMap.put("id_in", pinLeiIdsArray);
            List<RecycleType> recycleTypes = recycleTypeService.getList(hashMap, null);
            recycleBillTypeItemService.getRepository().deleteToUpdate(item.getId());
            for (RecycleType temp : recycleTypes) {
                RecycleBillTypeItem typeItem = new RecycleBillTypeItem();
                //赋值到typeItem并保存
                typeItem.setBill(item);
                typeItem.setName(temp.getName());
                typeItem.setDescription(temp.getDescription());
                typeItem.setNodePath(temp.getNodePath());
                typeItem.setSort(temp.getSort());
                typeItem.setLevel(temp.getLevel());
//                typeItem.setMinPrice(temp.getMinPrice());
//                typeItem.setMaxPrice(temp.getMaxPrice());
                typeItem.setImg(temp.getImg());
                typeItem.setFlag(1);
                recycleBillTypeItemService.saveAndFlush(typeItem);
            }
        }

        return R.ok(item);
    }

    @ApiOperation("回收单状态修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/saveByJsonHuiShouStatus", method = RequestMethod.POST)
    public Result saveByJsonHuiShou(String json, @CurrentUser UserVo userVo) {
        List<RecycleBill> recycleBills = JSON.parseArray(json, RecycleBill.class);
        AuthUser createUser = userService.findOne(userVo.getId());
        for (RecycleBill bill : recycleBills) {
            bill.setReceiveUser(createUser);
        }
        recycleBillService.save(recycleBills);
        if (recycleBills != null && recycleBills.size() > 0) {
            for (RecycleBill bill : recycleBills) {
                List<RecycleBillTypeItem> typeItems = bill.getTypeList();
                List<RecycleBillImgItem> imgItems = bill.getImgItemList();
                if (typeItems.size() > 0) {
                    for (RecycleBillTypeItem typeItem : typeItems) {
                        typeItem.setBill(bill);
                        recycleBillTypeItemService.save(typeItem);
                    }
                }
                if (imgItems.size() > 0) {
                    for (RecycleBillImgItem imgItem : imgItems) {
                        imgItem.setBill(bill);
                        recycleBillImgItemService.save(imgItem);
                    }
                }
            }
        }
        return R.ok(recycleBills);
    }


    @ApiOperation("上门收货完成，保存回收单最终信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/saveByJsonHuiShouComplete", method = RequestMethod.POST)
    public Result saveByJsonHuiShouComplete(String json, String pinLeiIds, String recycleBillTypeItemListJson, String imgItemJson, @CurrentUser UserVo userVo) throws IOException {
        String[] pinLeiIdsArray = null;
        List<String> imgItemList = null;
        List<RecycleBillTypeItem> typeItemList = null;
        ObjectMapper mapper = new ObjectMapper();
//        if (StringUtils.isNotEmpty(pinLeiIds)) {
//            pinLeiIdsArray = pinLeiIds.split(",");
//        }

        RecycleBill entity = JSON.parseObject(json, RecycleBill.class);
        if (entity.getId() == null) {//只有创建的时候才同步创建楼栋
        } else {
        }
        AuthUser createUser = userService.findOne(userVo.getId());
        entity.setReceiveUser(createUser);
        RecycleBill item = recycleBillService.save(entity);
        if (item != null) {
            List<RecycleBillTypeItem> typeItems = entity.getTypeList();
            List<RecycleBillImgItem> imgItems = entity.getImgItemList();
            if (typeItems != null && typeItems.size() > 0) {
                for (RecycleBillTypeItem typeItem : typeItems) {
                    typeItem.setBill(entity);
                    recycleBillTypeItemService.save(typeItem);
                }
            }
            if (imgItems != null && imgItems.size() > 0) {
                for (RecycleBillImgItem imgItem : imgItems) {
                    imgItem.setBill(entity);
                    recycleBillImgItemService.save(imgItem);
                }
            }
        }

        if (StringUtils.isNotEmpty(recycleBillTypeItemListJson)) {
            typeItemList = JSON.parseArray(recycleBillTypeItemListJson, RecycleBillTypeItem.class);
            for (RecycleBillTypeItem typeItem : typeItemList) {
                if (typeItem.getId() == null) {
                    typeItem.setBill(item);
                    typeItem.setFlag(1);
                    recycleBillTypeItemService.save(typeItem);
                }
            }
        }
        if (StringUtils.isNotEmpty(imgItemJson)) {
            imgItemList = mapper.readValue(imgItemJson, List.class);
            for (int i = 0; i < imgItemList.size(); i++) {
                RecycleBillImgItem imgItem = new RecycleBillImgItem();
                imgItem.setBill(item);
                imgItem.setName(imgItemList.get(i));
                imgItem.setFlag(1);
                recycleBillImgItemService.save(imgItem);
            }
        }
        return R.ok(item);
    }


    @ApiOperation("回收时计算价格和重量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/calculate-recycle", method = RequestMethod.POST)
    public Result calculateRecycle(String recycleBillTypeItemListJson) {
        List<RecycleBillTypeItem> typeItemList = null;
        BigDecimal tempPriceActual = BigDecimal.ZERO;
        BigDecimal tempWeightActual = BigDecimal.ZERO;
        if (StringUtils.isNotEmpty(recycleBillTypeItemListJson)) {
            typeItemList = JSON.parseArray(recycleBillTypeItemListJson, RecycleBillTypeItem.class);
            for (RecycleBillTypeItem typeItem : typeItemList) {
                BigDecimal itemPrice = typeItem.getPriceActual();
                BigDecimal itemWeight = typeItem.getWeightActual();
                if (itemPrice == null) {
                    itemPrice = BigDecimal.ZERO;
                }
                if (itemWeight == null) {
                    itemWeight = BigDecimal.ZERO;
                }

                tempPriceActual = tempPriceActual.add(itemPrice.multiply(itemWeight));
                // 注意单位变化，统一单位后再计算
                if (1 == typeItem.getUnitType()) {
                    // 如果价格单位是“元/吨”，将重量转换为克
                    itemWeight = itemWeight.multiply(new BigDecimal(1000));
                }
                tempWeightActual = tempWeightActual.add(itemWeight);
            }
        }
        // 设置价格和重量，保留4位小数
        BigDecimal priceActual = tempPriceActual.setScale(4, RoundingMode.HALF_UP);
        BigDecimal weightActual = tempWeightActual.setScale(4, RoundingMode.HALF_UP);

        HashMap hashMap = new HashMap();
        hashMap.put("priceActual",priceActual);
        hashMap.put("weightActual",weightActual);

        return R.ok(hashMap);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "json", value = "String", defaultValue = ""),
    })
    @RequestMapping(value = "/saveByJson-type", method = RequestMethod.POST)
    public Result saveByJson(String json) {
        RecycleType entity = JSON.parseObject(json, RecycleType.class);

        //子类不取传过来的值，只替换父类
        List<RecycleType> children = new ArrayList<>();
        if (entity.getId() != null) {
            RecycleType type1 = recycleTypeService.findOne(entity.getId());
//            entity.setFlag(type1.getFlag());
            entity.setCreateTime(type1.getCreateTime());
            children = type1.getChildren();

            if (children != null) {
                for (RecycleType type : children) {//修改的父类不能指向自己的子类避免进入死循环
                    if (entity.getParent() != null && entity.getParent().getId() != null
                            && type.getId() != null && entity.getParent().getId().equals(type.getId())) {
                        return null;
                    }
                }

            }
        } else {
            children = entity.getChildren();
            entity.setFlag(1);
            entity.setSort(9999);
            entity.setCreateTime(new Date());
            if (children != null)
                for (RecycleType type :
                        children) {//修改的父类不能指向自己的子类避免进入死循环
                    if (entity.getParent() != null && entity.getParent().getId() != null
                            && type.getId() != null && entity.getParent().getId().equals(type.getId())) {
                        return null;
                    }
                }
        }
        entity.setChildren(children);
        entity = recycleTypeService.save(entity);
        if (entity.getParent() != null && entity.getParent().getId() != null) {
            entity.setParent(recycleTypeService.findOne(entity.getParent().getId()));
        } else {
            entity.setParent(null);
        }
        //存储完后分析一次所有受影响的节点路劲再重新存储一次
        entity.setNodePath(recycleTypeService.getNodePath(entity, ""));
        recycleTypeService.sortType(entity);
        recycleTypeService.setChildNodePath(entity.getChildren());
        recycleTypeService.sortChildNodePath(entity.getChildren());
        entity.setLevel(recycleTypeService.getTypeLevel(entity));
        entity = recycleTypeService.save(entity);

        return R.ok(entity);
    }

    @ApiOperation("批量删除启用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "String", defaultValue = ""),
            @ApiImplicitParam(name = "operate", value = "1.删除 2.启用", defaultValue = ""),
    })
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    public Result batchDelete(Long[] ids, Integer operate) {
        if (ParamsUtils.isInputEmpty(ids, operate)) {
            return R.returnCode(NetCode.CODE_INPUT_OMIT);
        }
        for (int i = 0; i < ids.length; i++) {
            RecycleType authUser = recycleTypeService.findOne(ids[i]);
            if (authUser != null) {
                if (operate == 1) {
                    authUser.setFlag(0);
                } else if (operate == 2) {
                    authUser.setFlag(1);
                }
                recycleTypeService.save(authUser);
            }
        }
        return R.ok();
    }


    @ApiOperation(value = "逻辑删除", notes = "参数为JSON数据对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "long", paramType = "query"),
    })
    @RequestMapping(value = "/deleteByLogic", method = RequestMethod.POST)
    public Result deleteByLogic(Long id) {
        recycleTypeService.deleteById(id);
        return R.ok();
    }

    @ApiOperation(value = "逻辑删除批量", notes = "参数为JSON数据对象")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "8,28", dataType = "long", paramType = "query"),
    })
    @RequestMapping(value = "/deletesByLogic", method = RequestMethod.POST)
    public Result deletesByLogic(Long[] ids) {
        recycleTypeService.deleteByIds(ids);
        return R.ok();
    }


    @ApiOperation("批量更新顺序")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "originId", value = "原id"),
            @ApiImplicitParam(name = "relId", value = "变动后相对变化的id"),
            @ApiImplicitParam(name = "type", value = "变化 类型 before  after  inner"),
    })
    @RequestMapping(value = "/updateSort", method = RequestMethod.POST)
    public Result updateSort(Long originId, Long relId, String type) {
        if (ParamsUtils.isInputEmpty(originId, relId, type)) {
            return R.returnCode(NetCode.CODE_INPUT_OMIT);
        }
        RecycleType originType = recycleTypeService.getById(originId);
        RecycleType relType = recycleTypeService.getById(relId);

        if (originType == null || relType == null) {
            return R.fail("操作失败");
        }
        if ("before".equals(type) || "after".equals(type)) {
            originType.setLevel(relType.getLevel());
            originType.setParent(relType.getParent());
//            //设置排序位置
            originType.setSort(relType.getSort());
            //查询同等级下相同类型的分类列表进行一次排序
            HashMap hashMap = new HashMap();
            hashMap.put("level", relType.getLevel());
            if (relType.getParent() != null)
                hashMap.put("parent.id", relType.getParent().getId());

            PageableRequest pageableRequest = new PageableRequest();
            pageableRequest.setSort(SortUtils.addSort(new Sort("sort", "asc")));
            List<RecycleType> list = recycleTypeService.getList(hashMap, pageableRequest);

            //先移除
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getId() == originType.getId()) {
                    list.remove(i--);
                }
            }

            int relIndex = 0;
            for (int i = 0; i < list.size(); i++) {
//                if (relType.getSort() == list.get(i).getSort()) {
//                    list.get(i).setSort(i);
//                }
                if (relType.getId() == list.get(i).getId()) {
                    relIndex = i;
                }
            }

            if ("before".equals(type)) {
                list.add(relIndex, originType);
            } else if ("after".equals(type)) {
                list.add(relIndex + 1, originType);
            }

            for (int i = 0; i < list.size(); i++) {
                list.get(i).setSort(i);
            }

            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i).getName() + "   " + list.get(i).getSort());
            }
            recycleTypeService.save(list);
            recycleTypeService.save(originType);
        } else if ("inner".equals(type)) {
            originType.setLevel(relType.getLevel() + 1);
            originType.setParent(relType);
//            //设置排序位置
            originType.setSort(relType.getSort());
            //查询同等级下相同类型的分类列表进行一次排序
            HashMap hashMap = new HashMap();
            hashMap.put("level", relType.getLevel() + 1);
            if (relType.getParent() != null)
                hashMap.put("parent.id", relType.getId());

            PageableRequest pageableRequest = new PageableRequest();
            pageableRequest.setSort(SortUtils.addSort(new Sort("sort", "asc")));
            List<RecycleType> list = recycleTypeService.getList(hashMap, pageableRequest);

            //先移除
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getId() == originType.getId()) {
                    list.remove(i--);
                }
            }

            list.add(originType);

            for (int i = 0; i < list.size(); i++) {
                list.get(i).setSort(i);
            }

            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i).getName() + "   " + list.get(i).getSort());
            }
            recycleTypeService.save(list);
            recycleTypeService.save(originType);
        }


        RecycleType entity = originType.getParent();
        if (entity != null) {//1级分类无需处理子分类的节点数据
            //存储完后分析一次所有受影响的节点路劲再重新存储一次
            entity.setNodePath(recycleTypeService.getNodePath(entity, ""));
            recycleTypeService.sortType(entity);
            recycleTypeService.setChildNodePath(entity.getChildren());
            recycleTypeService.sortChildNodePath(entity.getChildren());
            entity.setLevel(recycleTypeService.getTypeLevel(entity));
        }

        return R.ok();
    }

}

